package cbnu.Imvision.BeWithYou.Tour;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import cbnu.Imvision.BeWithYou.R;
import cbnu.Imvision.BeWithYou.Care.CareMap;
import cbnu.Imvision.BeWithYou.Reserve.Reserve;

public class TourDetail extends Activity {

	String sJson;
	String selectedLoc;
	Double selectedLat;
	Double selectedLon;

	TextView tourDetailName;
	TextView tourDetailCategory;
	TextView tourDetailArea;
	TextView tourDetailTel;
	TextView tourDetailAddress;
	TextView tourDetailExplain;
	ImageView tourDetailPicture;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tour_detail);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		sJson = getIntent().getStringExtra("sJsonTour");
		selectedLoc = getIntent().getStringExtra("selectedLoc");
		if (selectedLoc != null) {
			selectedLat = Double.parseDouble(selectedLoc.substring(0, selectedLoc.indexOf(", ")));
			selectedLon = Double.parseDouble(selectedLoc.substring(selectedLoc.indexOf(", ") + 2));
		}
		
		tourDetailName = (TextView) findViewById(R.id.tourDetailName);
		tourDetailArea = (TextView) findViewById(R.id.tourDetailArea);
		tourDetailTel = (TextView) findViewById(R.id.tourDetailTel);
		tourDetailAddress = (TextView) findViewById(R.id.tourDetailAddress);
		tourDetailExplain = (TextView) findViewById(R.id.tourDetailExplain);
		tourDetailPicture = (ImageView) findViewById(R.id.tourDetailPicture);

		setDetail(sJson);
	}

	public void tourMap(View v) {
		Intent intent = new Intent(getBaseContext(), TourMap.class);
		intent.putExtra("sJsonTour", sJson);
		intent.putExtra("selectedLoc", selectedLoc);
		startActivity(intent);
	}

	public void tourReserve(View v) {
		Intent intent = new Intent(getBaseContext(), Reserve.class);
		intent.putExtra("selectedArrival", tourDetailAddress.getText().toString() + " "
				+ tourDetailName.getText().toString());
		startActivity(intent);
	}

	public void setDetail(String sJson) {
		try {
			JSONArray ja = new JSONArray(sJson);
			for (int j = 0; j < ja.length(); j++) {
				JSONObject order = ja.getJSONObject(j);
				if (selectedLat == order.getDouble("lat") && selectedLon == order.getDouble("lon")) {
					tourDetailName.setText(order.getString("name"));
					tourDetailArea.setText(order.getString("area"));
					tourDetailTel.setText(order.getString("tel"));
					tourDetailAddress.setText(order.getString("address"));
					tourDetailExplain.setText(order.getString("explain"));

					Bitmap bitmap = BitmapFactory.decodeFile("/data/data/cbnu.Imvision.BeWithYou/files/"
							+ order.getString("pic_url"));
					bitmap = Bitmap
							.createScaledBitmap(
									bitmap,
									config.Config.SIZE_OF_DETAIL_ImageView,
									(int) (config.Config.SIZE_OF_DETAIL_ImageView * ((double) bitmap.getHeight() / (double) bitmap
											.getWidth())), false);
					tourDetailPicture.setImageBitmap(bitmap);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_tour_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
